package sf.model.fields;

import javax.swing.event.EventListenerList;

import sf.model.fields.event.FieldChangeEvent;
import sf.model.fields.event.FieldChangeListener;

/*******************************************************************************
 * Module: StandardFormField.java Author: Petar Purpose: Defines the Class
 * StandardFormField
 ******************************************************************************/

public abstract class AbstractFieldModel {
    private EventListenerList listenerList = new EventListenerList();
    FieldChangeEvent changeEvent = null;

    public void fireChanged() {
	Object[] listeners = listenerList.getListenerList();
	for (int i = listeners.length - 2; i >= 0; i -= 2) {
	    if (listeners[i] == FieldChangeEvent.class) {
		// Lazily create the event:
		if (changeEvent == null)
		    changeEvent = new FieldChangeEvent(this);
		((FieldChangeListener) listeners[i + 1])
			.changePerformed(changeEvent);
	    }
	}
    }

    /**
     * @param listener
     * @pdOid 9920fa53-5c52-4b28-b838-d8f587859309
     */
    public void addChangeListener(FieldChangeListener l) {
	Object[] listeners = listenerList.getListenerList();
	boolean contains = false;
	for (int i = 0; i < listeners.length; i++) {
	    if (listeners[i] == l) {
		contains = true;
		break;
	    }
	}

	if (!contains)
	    listenerList.add(FieldChangeListener.class, l);
    }

    /** @pdOid fd0f2a48-7e1a-4f3e-9712-79a221f0f578 */
    public abstract Object getValue();

    /**
     * @param value
     * @pdOid f13f358f-1b4b-49de-91b3-5fa1c9d5175c
     */
    public abstract void setValue(Object value);

    /** @pdOid 3dffda6a-a6d4-4a4a-a32d-aea0064e464c */
    public void clear() {
	setValue(null);
	fireChanged();
    }

}